﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using NetDimension.NanUI;

namespace AlarmManagePlatform.Forms
{
    internal partial class MainForm : MetroFramework.Forms.MetroForm
    {
        internal event EventHandler Event_FormClosing;
        internal MainForm()
        {
            InitializeComponent();
            //气体报警器智能监测管理平台
            this.Text = "现场智能便携式气体检测仪监测系统";
            this.WindowState = FormWindowState.Maximized;
            this.MinimizeBox = true;
            this.FormClosing += MainForm_FormClosing;
            this.Load += MainForm_Load;
            labUserInfo.Text = $"用户：{Static.ParaInfo.CurrentUser.userName}    角色：{Static.ParaInfo.CurrentUser.roleName}";
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            try
            {
                ////这个文件于可执行文件放在同一目录
                //webBrowser1.Url = new Uri(Path.Combine(Application.StartupPath, "Resources\\Baidu.html"));
                btnHostPage.PerformClick();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "异常", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            Static.Methods.SaveConfig();

            Event_FormClosing?.Invoke(sender, e);
        }

        private void btnUserMng_Click(object sender, EventArgs e)
        { 
            UserMng userMng = new UserMng();
            userMng.ShowDialog(this);
        }

        private UC.TaskManagement TaskManagement;
        private UC.Alarm.AlarmManagement AlarmManagement;
        private UC.HistoryInfo HistoryInfo;
        private UC.ReportStatistics ReportStatistics;
        private UC.InspectionDepartmen InspectionDepartmen;
        private UC.CollectTerminal CollectTerminal;
        UC.Map HostPage;
        private void btnTask_Click(object sender, EventArgs e)
        {
            if (pMain.Controls.Count > 0 && pMain.Controls[0] as UC.TaskManagement != null)
            {
                return;
            }

            pMain.Controls.Clear();

            if (TaskManagement == null)
                TaskManagement = new UC.TaskManagement();
            TaskManagement.Dock = DockStyle.Fill;

            pMain.Controls.Add(TaskManagement);

            UpdateButtonColor(btnTask);
        }

        private void btnAlarmInfo_Click(object sender, EventArgs e)
        {
            if (pMain.Controls.Count > 0 && pMain.Controls[0] as UC.Alarm.AlarmManagement != null)
            {
                return;
            }

            pMain.Controls.Clear();

            if (AlarmManagement == null)
                AlarmManagement = new UC.Alarm.AlarmManagement();
            
            AlarmManagement.Dock = DockStyle.Fill;

            pMain.Controls.Add(AlarmManagement);

            UpdateButtonColor(btnAlarmInfo);
        }

        private void btnHistory_Click(object sender, EventArgs e)
        {
            if (pMain.Controls.Count > 0 && pMain.Controls[0] as UC.HistoryInfo != null)
            {
                return;
            }

            pMain.Controls.Clear();

            if (HistoryInfo == null)
                HistoryInfo = new UC.HistoryInfo();
            HistoryInfo.Dock = DockStyle.Fill;

            pMain.Controls.Add(HistoryInfo);

            UpdateButtonColor(btnHistory);
        }

        private void btnHostPage_Click(object sender, EventArgs e)
        {
            if (pMain.Controls.Count > 0 && pMain.Controls[0] as UC.Map != null)
            {
                return;
            }

            pMain.Controls.Clear();

            if (HostPage == null)
            {
                HostPage = new UC.Map();
                HostPage.Dock = DockStyle.Fill;
            }

            pMain.Controls.Add(HostPage);

            UpdateButtonColor(btnHostPage);
        }

        private void btnVertificationRegulation_Click(object sender, EventArgs e)
        {
            SimplifiedProcess simplifiedProcess = new SimplifiedProcess();
            simplifiedProcess.ParaInfoList = null;
            simplifiedProcess.ShowDialog(this);
        }

        private void btnReportStatistics_Click(object sender, EventArgs e)
        {
            if (pMain.Controls.Count > 0 && pMain.Controls[0] as UC.ReportStatistics != null)
            {
                return;
            }

            pMain.Controls.Clear();

            if (ReportStatistics == null)
                ReportStatistics = new UC.ReportStatistics();
            ReportStatistics.Dock = DockStyle.Fill;

            pMain.Controls.Add(ReportStatistics);

            UpdateButtonColor(btnReportStatistics);
        }

        private void btnSubmitUnit_Click(object sender, EventArgs e)
        {
            //InspectionDepartmen
            if (pMain.Controls.Count > 0 && pMain.Controls[0] as UC.InspectionDepartmen != null)
            {
                return;
            }

            pMain.Controls.Clear();

            if (InspectionDepartmen == null)
                InspectionDepartmen = new UC.InspectionDepartmen();
            InspectionDepartmen.Dock = DockStyle.Fill;

            pMain.Controls.Add(InspectionDepartmen);

            UpdateButtonColor(btnSubmitUnit);
        }

        private void btnCollectTerminal_Click(object sender, EventArgs e)
        {
            //CollectTerminal
            //if (pMain.Controls.Count > 0 && pMain.Controls[0] as UC.CollectTerminal != null)
            //{
            //    return;
            //}

            //pMain.Controls.Clear();

            //if (CollectTerminal == null)
            //    CollectTerminal = new UC.CollectTerminal();
            //CollectTerminal.Dock = DockStyle.Fill;
            //pMain.Controls.Add(CollectTerminal);

            Forms.CollectTerminalMng _CollectTerminalMng = new CollectTerminalMng();
            _CollectTerminalMng.ShowDialog(this);
        }

        private void btnSetup_Click(object sender, EventArgs e)
        {
            Forms.SetupForm _SetupForm = new SetupForm();
            _SetupForm.ShowDialog(this);
        }

        private void UpdateButtonColor(DevComponents.DotNetBar.ButtonX Sender)
        {
            lFunctionName.Text = "（" + Sender.Text + "）";
            DevComponents.DotNetBar.ButtonX _Temp;
            for (int i = 0; i < metroPanel1.Controls.Count; i++)
            {
                _Temp = metroPanel1.Controls[i] as DevComponents.DotNetBar.ButtonX;
                if (_Temp == null)
                    continue;
                _Temp.BackColor = Color.Transparent;
            }
            Sender.BackColor = Color.SlateBlue;
        }
    }
}